package 力扣题库;

import java.util.LinkedList;
import java.util.Stack;

public class p232用栈实现队列 {
    class MyQueue {
        Stack<Integer> s1;//用来入队列
        Stack<Integer> s2;//用来出队列

        public MyQueue() {
            s1=new Stack<>();
            s2=new Stack<>();
        }

        public void push(int x) {
            //入队列 始终进栈s1
            s1.push(x);
        }

        public int pop() {
            //出队列
            //当s2为空时，将s1中全部倒给s2
            //始终弹出s2的栈顶元素
            if (empty()) return -1;//两个栈为空 返回-1
            if (s2.isEmpty()) {
                int size=s1.size();
                for (int i = 0; i <size; i++) {
                    int x=s1.pop();
                    s2.add(x);
                }
            }
            return s2.pop();

        }

        public int peek() {
            //查看队列的队头元素
            if (empty()) return -1;
            if (s2.isEmpty()) {
                int size=s1.size();
                for (int i = 0; i <size; i++) {
                    int x=s1.pop();
                    s2.add(x);
                }
            }
           return s2.peek();

        }

        public boolean empty() {
            return s1.isEmpty() && s2.isEmpty();
        }
    }
}
